﻿using System;
using System.Collections.Generic;
using System.Linq;

using SharedGenomics.Core;
using SharedGenomics.Annotations;

namespace SharedGenomics.Workbench.Annotations
{
    /// <summary>
    /// Represents a set of completed annotations of a particular type. 
    /// </summary>
    public class AnnotationPool
    {
        private readonly IDictionary<object, AnnotationPoolItem> _itemMap;
        private readonly IDictionary<AnnotationValue, AnnotationPoolItem> _parentMap;

        private readonly object _itemMapLock = new object();
        private readonly object _parentMapLock = new object();

        public AnnotationPool()
        {
            this._itemMap = new Dictionary<object, AnnotationPoolItem>();
            this._parentMap = new Dictionary<AnnotationValue, AnnotationPoolItem>();
        }

        public void Add(object value, AnnotationValue parentValue)
        {
            value.ThrowIfNull("value");

            var poolItem = new AnnotationPoolItem(value) { ParentValue = parentValue };

            lock (this._itemMapLock)
            {
                this._itemMap[value] = poolItem;
            }

            if (parentValue != null)
            {
                lock (this._parentMapLock)
                {
                    this._parentMap[parentValue] = poolItem;
                }
            }
        }

        public AnnotationPoolItem this[object value]
        {
            get
            {
                if (value == null) return null;

                lock (this._itemMapLock)
                {
                    return this._itemMap.ContainsKey(value) ? this._itemMap[value] : null;
                }
            }
        }

        public AnnotationPoolItem GetForParent(AnnotationValue parentValue)
        {
            if (parentValue == null) return null;

            lock (this._parentMapLock)
            {
                return this._parentMap.ContainsKey(parentValue) ? this._parentMap[parentValue] : null;
            }
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }
    }
}
